//图片帧的大小，中心点
class frameData {
  constructor() {
    this.width = this.height = this.centerX = this.centerY = 0;
  }
}

class modal {
  constructor(stageName, stageRes, m) {
      this.layer = m.layer;
      this.width = m.width;
      this.height = m.height;
      this.classid = m.classid;
      this.flag = m.flag;
      this.group = m.groupid;
      //有些数据是可变的，丢这里面
      this.vdata = m;
	  
    this.frames = [];
    this.framesInfo = [];
    //资源文件名
    let resFile = m.imgfile.split('.')[0];
    //资源文件内容
    let json = stageRes[resFile];
    if(!json || !json.image || !json.image.file) {
    	console.log("error loading modal : " + m.id + " imgfile " + m.imgfile)
    	return;
    }
      
    let imgFile = stageName + '/' + json.image.file.split(':')[0];
    this.image_count_per_dir = json.image.count_per_dir ? parseInt(json.image.count_per_dir) : 1;
    if (json.rect_list) {
      try {
        for (let rect of json.rect_list) {
          let sp_num = rect.split(':');
          if (sp_num.length < 4) break;
          let data = new frameData();
          let x = parseInt(sp_num[0]);
          let y = parseInt(sp_num[1]);
          data.width = parseInt(sp_num[2]); data.centerX = data.width / 2;
          data.height = parseInt(sp_num[3]); data.centerY = data.height / 2;
          //默认中心点是图片正中，配置可以指定其它的中心点
          if(sp_num.length >= 6) {
            data.centerX = parseInt(sp_num[4]) || data.centerX;
            data.centerY = parseInt(sp_num[5]) || data.centerY;
          }
          //自动分割图片，不用按rect_list一行行加
          if(sp_num.length > 6) {
            let count_w = parseInt(sp_num[6]);
            let count_h = parseInt(sp_num[7]);
            for(let j = 0; j < count_h; j++) {
              for(let i = 0; i < count_w; i++){
                this.frames.push(SpriteUtil.frame(imgFile, x + i * data.width, y + j * data.height, data.width, data.height));
                this.framesInfo.push(data);
              }
            }
          } else {
            this.frames.push(SpriteUtil.frame(imgFile, x, y, data.width, data.height));
            this.framesInfo.push(data);
          }
        }
      }catch (e) {
    	  console.log("modal:" + m.id + " error:" + e)
      }
      this.image_interval = json.image.interval || 1;
      this.dir_count = Math.floor(this.frames.length / this.image_count_per_dir);
      // 碰撞检测矩形，对象死亡时管理器清除空
      this.hit_rects = [];
      	if("x" in m)
      		this.hit_rects.push({x : m.x, y : m.y, w : m.w, h : m.h});
    }

   this.sound = [];
    if(json.sound_list) {
      for (let sound of json.sound_list) {
        this.sound.push(game.stageSound[sound]);
      }
    }
  }

  haveshadow(){ return (this.flag & objectFlag.TFLAG_HAVE_SHADOW) > 0; }
  startparent(){ return (this.flag & objectFlag.TFLAG_START_PARENT) > 0; }
  stickparent(){ return (this.flag & objectFlag.TFLAG_STICK_PARENT) > 0; }
  dieparent(){ return (this.flag & objectFlag.TFLAG_DIE_PARENT) > 0; }
  winkred(){ return (this.flag & objectFlag.TFLAG_WINK_RED) > 0; }
  winkwhite(){ return (this.flag & objectFlag.TFLAG_WINK_WHITE) > 0; }
}